<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      let arr1 = [1, 2, 3, 4]
      let arr2 = [3, 4, 5]

      function getSort(arr1, arr2) {
        let [i, j] = [0, 0]
        let newArr = []
        
        while (i < arr1.length && j < arr2.length) {
          if (arr1[i] < arr2[j]) {
            newArr.push(arr1[i])
            i++
          } else if (arr1[i] > arr2[j]) {
            newArr.push(arr2[j])
            j++
          } else if (arr1[i] === arr2[j]) {
            newArr.push(arr1[i])
            newArr.push(arr2[j])
            i++, j++
          }
        }
        //剩余部分加到新数组最后
        if (i < arr1.length) {
          return newArr.concat(arr1.splice(i))
        } else if (j < arr2.length) {
          return newArr.concat(arr2.splice(j))
        } else {
          return newArr
        }
      }
      console.log(getSort(arr1, arr2))
    </script>
  </body>
</html>
